package com.example.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.ResultCode;
import com.example.dao.*;
import com.example.entity.Rukuxinxi;
import com.example.exception.CustomException;
import com.example.service.RukuxinxiService;
import com.example.vo.RukuxinxiVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Map;

@Service("rukuxinxiService")
public class RukuxinxiServiceImpl extends ServiceImpl<RukuxinxiDao, Rukuxinxi> implements RukuxinxiService {

    @Resource
    private RukuxinxiDao rukuxinxiDao;

    public Rukuxinxi add(Rukuxinxi rukuxinxi) {
		
	   //youquchong1
        rukuxinxi.setAddtime(new Date());
        rukuxinxiDao.insert(rukuxinxi);
        return rukuxinxi;
    }
	
	
	
    public void delete(Long id) {
        rukuxinxiDao.deleteById(id);
    }
	

    public void update(Rukuxinxi rukuxinxi) {
        //youquchong2
        rukuxinxiDao.updateById(rukuxinxi);
    }
	
	public void updateByzhujian(Rukuxinxi rukuxinxi) {
        LambdaQueryWrapper<Rukuxinxi> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtils.isNotEmpty(rukuxinxi.getBianhao()), Rukuxinxi::getBianhao,rukuxinxi.getBianhao());

        rukuxinxiDao.update(rukuxinxi, wrapper);
    }
	
	@Override
    public List<Map<String, Object>> daochuexcel() {
        return rukuxinxiDao.daochuexcel();
    }

    @Override
    public Rukuxinxi findById(Integer id) {
        return rukuxinxiDao.selectById(id);
    }
	
	@Override
    public Rukuxinxi getByzhujian(String zhujian) {
        return rukuxinxiDao.getByzhujian(zhujian);
    }

    public RukuxinxiVo findPage(RukuxinxiVo rukuxinxiVo) {
        LambdaQueryWrapper<Rukuxinxi> wrapper = Wrappers.lambdaQuery();
		wrapper.like(ObjectUtils.isNotEmpty(rukuxinxiVo.getBianhao()), Rukuxinxi::getBianhao,rukuxinxiVo.getBianhao());        wrapper.like(ObjectUtils.isNotEmpty(rukuxinxiVo.getMingcheng()), Rukuxinxi::getMingcheng,rukuxinxiVo.getMingcheng());        wrapper.like(ObjectUtils.isNotEmpty(rukuxinxiVo.getLeibie()), Rukuxinxi::getLeibie,rukuxinxiVo.getLeibie());        wrapper.like(ObjectUtils.isNotEmpty(rukuxinxiVo.getKucun()), Rukuxinxi::getKucun,rukuxinxiVo.getKucun());        wrapper.like(ObjectUtils.isNotEmpty(rukuxinxiVo.getRukushuliang()), Rukuxinxi::getRukushuliang,rukuxinxiVo.getRukushuliang());        wrapper.like(ObjectUtils.isNotEmpty(rukuxinxiVo.getRukushijian()), Rukuxinxi::getRukushijian,rukuxinxiVo.getRukushijian());        wrapper.like(ObjectUtils.isNotEmpty(rukuxinxiVo.getBeizhu()), Rukuxinxi::getBeizhu,rukuxinxiVo.getBeizhu());        
		wrapper.orderByDesc(Rukuxinxi::getId);
        Page<Rukuxinxi> page = new Page<>(rukuxinxiVo.getCurrent(),rukuxinxiVo.getCurrentNum());
        rukuxinxiDao.selectPage(page,wrapper);
        List<Rukuxinxi> rukuxinxis = page.getRecords();
        rukuxinxiVo.setList(rukuxinxis);
        rukuxinxiVo.setPages(page.getPages());
        rukuxinxiVo.setTotal(page.getTotal());
        rukuxinxiVo.setCurrent(page.getCurrent());
        rukuxinxiVo.setCurrentNum(page.getSize());
        return rukuxinxiVo;
    }

    @Override
    public void deleteList(List<Rukuxinxi> list) {
        List<Integer> ids =  list.stream().map(Rukuxinxi::getId).collect(Collectors.toList());
        rukuxinxiDao.deleteBatchIds(ids);
    }
	
	
	
	
	
	//youxxianxci
}
